package cn.net.chenbao.medical;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import org.xutils.x;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import cn.net.chenbao.medical.api.Api;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.xutils.ParamsUtils;
import cn.net.chenbao.medical.xutils.WWXCallBack;

public class ResultActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mTvTitle,mTvSure;
    private boolean result;
    private long mComNo;
    private int mode;
    private TextView mTvTime;
    private final static int COUNT = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        InitTitle();
        initView();
    }
    private Handler handler = new Handler(){
        int num = 2;
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case COUNT:
                    mTvTime.setText(String.valueOf(num));
                    num--;
                    break;

                default:
                    break;
            }
        };
    };

    private void initView() {
        findViewById(R.id.tv_fail_index).setOnClickListener(this);
        findViewById(R.id.tv_success_index).setOnClickListener(this);
        findViewById(R.id.tv_fail_resubmit).setOnClickListener(this);
    }

    private void InitTitle() {
        result=getIntent().getBooleanExtra(Consts.KEY_MODULE,false);
        findViewById(R.id.iv_left).setOnClickListener(this);
        mTvTitle= (TextView) findViewById(R.id.tv_center);
        mTvSure= (TextView) findViewById(R.id.tv_sure);
        mTvTitle.setText(R.string.result);
        if(result){
            mTvTime= (TextView) findViewById(R.id.tv_success_time);
            findViewById(R.id.ll_success).setVisibility(View.VISIBLE);
            findViewById(R.id.ll_fail).setVisibility(View.GONE);

            //三秒倒计时
            final Timer timer = new Timer();
            final long end = System.currentTimeMillis() + 1000*3;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(COUNT);

                }
            }, 0, 1000);
            //这里的schedule的第二个参数意义是到了这个时间尽快运行run里面的方法
            timer.schedule(new TimerTask() {

                @Override
                public void run() {

                    finish();
                    timer.cancel();
                }
            }, new Date(end));
        }else {
            mComNo=getIntent().getLongExtra(Consts.KEY_DATA,-1);
            mode=getIntent().getIntExtra(Consts.KEY_MODE,-1);
            findViewById(R.id.ll_fail).setVisibility(View.VISIBLE);
            findViewById(R.id.ll_success).setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
            case R.id.tv_fail_index:
            case R.id.tv_success_index:{
                finish();
                break;
            }
            case R.id.tv_fail_resubmit:{
                SubmitInfo(mode);
                break;
            }
        }
    }

    private void SubmitInfo(int mode) {
        String url=null;
        String result=null;
        switch (mode){
            case Consts.MODE_NURSE:{
                url= Api.pyConfirm();
                result="PyConfirm";
                break;
            }
            case Consts.MODE_APOTHECARY:{
                url=Api.outConfirm();
                result="OutConfirm";
                break;
            }
            case Consts.MODE_DRUG_WITHDRAWAL:{
                url=Api.backConfirm();
                result="BackConfirm";
                break;
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SESSION_ID, MedicalApplication.getInstance().getSessionId());
        jsonObject.put(Consts.COMNO, mComNo);
        x.http().post(
                ParamsUtils.getPostJsonParams(jsonObject, url),
                new WWXCallBack(result) {

                    @Override
                    public void onAfterSuccessOk(JSONObject data) {
                        Intent intent=new Intent(ResultActivity.this,ResultActivity.class);
                        if(data.getBoolean("Data")){
                            intent.putExtra(Consts.KEY_MODULE,true);
                        }else {
                            intent.putExtra(Consts.KEY_MODULE,false);
                            intent.putExtra(Consts.KEY_DATA,mComNo);
                        }
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onAfterSuccessError(JSONObject data) {
                        super.onAfterSuccessError(data);

                    }

                    @Override
                    public void onAfterFinished() {

                    }
                });
    }
}
